04. Stack and Heap

C++ Memory MGMT 01 Introduction A02 Stack And Heap

Stack and Heap

Description

In general, memory management in C++ can be split into two major categories by their storage in the host machine. When we analyze this classification, we have two main concepts: stack and heap.

Comparison

The Stack

  1. only local variables
  2. resizing of variables cannot be done
  3. efficiently managed space by CPU, memory does not become fragmented
  4. very quick access
  5. there is a limit on stack size (OS-dependent)
  6. explicit de-allocation of variables is not mandatory

The Heap

  1. memory must be managed
  2. memory size is not limited
  3. access is relatively slower
  4. realloc() is used for the resizing of variables
  5. variables can be accessed globally
  6. efficient use of space is not guaranteed, memory can become fragmented over time when blocks of memory are allocated, then freed